package com.datastructure.test.lengthoflongestsubstring;

import java.util.HashMap;
import java.util.Map;

public class LengthOfLongestSubstring {

    public static void main(String[] args) {

    }

    public int lengthOfLongestSubstring (String s) {
        // write code here
        char[] charArray = s.toCharArray();
        if(charArray.length==0){
            return 0;
        }
        int max = 0;
        Map<Character,Integer> result = new HashMap<>();
        for(int i=0,j=0;i< charArray.length;i++){
            if(result.containsKey(charArray[i])){
                j = Math.max(j,result.get(charArray[i])+1);
            }
            result.put(charArray[i],i);
            max=Math.max(max,i-j+1);
        }
        return max;
    }
}
